<template>
  <view>
    <u-navbar :title="query.id ? (formdisabled ? '查看' : '修改') : '新增'">
      <template #right v-if="formdisabled">
        <view class="edtbtn" @click="bjfn()"> 编辑 </view>
      </template>
    </u-navbar>
    <formz ref="formz" :disabled="formdisabled" :inputs="inputs" :data="data"></formz>

    <bottombox v-if="!formdisabled">
      <template v-if="query.id">
        <u-button type="error" :loading="loading" text="删除" @click="ondel"></u-button>
        <view style="width: 40rpx"></view>
      </template>
      <u-button type="primary" :loading="loading" text="保存数据" @click="onedit"></u-button>
    </bottombox>
  </view>
</template>

<script>
import {
  requestAdd as add,
  requestUpdate as update,
  requestDetail as getInfo,
  requestDel as remove,
} from "@/api/contract/contractDraft";
import { getTypeList } from "@/api/projectManage/projectType";
export default {
  data() {
    return {
      data: {},
      loading: false,
      inputs: [],
      query: {},
      formdisabled: true,
    };
  },
  onLoad(query) {
    this.query = query;
    this.init();
  },
  methods: {
    async init() {
      console.log(
        this.$store.state.dict.dictDatas[
        this.$store.state.dict.DICT_TYPE.INTERNAL_ACCOUNT_EXPENDITURE_TYPE
        ]
      );
      if (this.query.id) {
        let data = await getInfo(this.query.id);
        this.data = data.data;
        this.formdisabled = true;
      } else {
        this.data = {};
        this.formdisabled = false;
      }
      let data = await getTypeList();
      this.inputs = [
        {
          label: "合同编号",
          value: "contractCode",
          rules: [{ required: true, message: "不能为空", trigger: "blur" }],
        },
        {
          label: "合同名称",
          value: "contractName",
          rules: [
            { required: true, message: "不能为空", trigger: "blur" },
            {
              pattern: "^[\u4e00-\u9fa5\\w]{4,30}$",
              message: "长度在4-30个字符之间，一个中文字等于2个字符",
              trigger: ["blur", "change"],
            },
          ],
        },
        {
          label: "合同类型",
          value: "contractType",
          rules: [{ required: true, message: "不能为空", trigger: "blur" }],
          type: "select",
          option: this.$store.state.dict.dictDatas["contract_type"],
        },
        {
          label: "所属项目",
          value: "projectCode",
          type: "select",
          option: data.data.map((item) => {
            return {
              label: item.typeName,
              value: item.typeCode,
            };
          }),
          rules: [
            {
              required: true,
              trigger: "blur",
              message: "所属项目不能为空",
            },
          ],
          onChange({ emit, activeOption }) {
            this.$emit("change", {
              typeName: activeOption.label,
            });
          },
        },
        {
          label: "合同金额",
          value: "amount",
          dw: "元",
          rules: [
            { required: true, message: "不能为空", trigger: "blur" },
            {
              pattern: "^-?\\d+(\\.\\d{1,2})?$",
              message: "请填写数字或最多两位小数的数字",
              trigger: ["blur", "change"],
            },
          ],
        },
        {
          label: "需要回款",
          value: "returned",
          rules: [{ required: true, message: "不能为空", trigger: "blur" }],
          type: "select",
          option: [
            { label: "是", value: "1" },
            { label: "否", value: "0" },
          ],
        },
        {
          label: "合同资料",
          value: "contractDocumentPath",
          type: "file",
          attrs: {
            accept: "all",

          },
        },

        {
          label: "合同相关人员信息",
          type: "tag",
        },
        {
          label: "合同负责人",
          value: "contractManager",
        },
        {
          label: "负责人电话",
          value: "contractManagerPhone",
          rules: [
            { required: false, message: "不能为空", trigger: "blur" },
            {
              pattern: "^1[3456789]\\d{9}$",
              message: "请填写正确的手机号码",
              trigger: ["blur", "change"],
            },
          ],
        },
        {
          label: "合同联系人",
          value: "contractContact",
        },
        {
          label: "联系人电话",
          value: "contractContactPhone",
          rules: [
            { required: false, message: "不能为空", trigger: "blur" },
            {
              pattern: "^1[3456789]\\d{9}$",
              message: "请填写正确的手机号码",
              trigger: ["blur", "change"],
            },
          ],
        },
        {
          label: "合同申请人",
          value: "contractApplicantName",
        },
        {
          label: "客户单位",
          value: "customerName",
        },
        {
          label: "合同相关时间信息",
          type: "tag",
        },
        {
          label: "申请时间",
          value: "contractApplicationTime",
          type: "datetime",

        },
        {
          label: "签订时间",
          value: "signingDate",
          type: "datetime",

        },
        {
          label: "开始日期",
          value: "performanceDateStart",
          type: "datetime",
          attrs: {
            endDate: "2099-12-31",
            showFormDate: "0",
          },
        },
        {
          label: "结束日期",
          value: "performanceDateEnd",
          type: "datetime",
          attrs: {
            endDate: "2099-12-31",
            showFormDate: "0",
          },
        },
        {
          label: "其他信息",
          type: "tag",
        },
        {
          label: "备注",
          value: "notes",
          type: "textarea"
        },
      ];
    },
    async ondel() {
      uni.showModal({
        title: "提示",
        content: "确定删除此数据？",
        success: async (res) => {
          if (res.confirm) {
            this.loading = true;
            await remove([this.data.uuid]);
            uni.showToast({
              title: "操作成功",
              icon: "none",
              duration: 2000,
            });
            this.loading = false;
            uni.navigateBack();
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });
    },
    async onedit() {
      let data = await this.$refs.formz.validate();

      this.loading = true;
      try {
        if (this.query.id) {
          await update(data);
          await this.$u.modal("修改成功");
        } else {
          data.contractSource = "DRAFT"
          await add(data);
          await this.$u.modal("添加成功");
        }
        this.loading = false;
        this.$u.back();
      } catch (error) {
        this.loading = false;
      }
    },
    bjfn() {
      this.formdisabled = false;
    },
  },
};
</script>

<style>
page {
  background: #fff;
}
</style>
<style lang="scss" scoped>
.edtbtn {
  color: #1890ff;
  font-size: 28rpx;
}
</style>
